﻿CREATE FUNCTION [dbo].[Borrower]
(	@LoanID int = -1,
	@Pos int     = -1
)RETURNS TABLE WITH SCHEMABINDING
AS
RETURN 
SELECT     B.ID, L_B.LoanID, B.FirstName, B.LastName, B.FirstName + ' ' + B.LastName AS FullName, B.SSN, B.PhoneHome, B.DOBMonth, B.DOBDay, 
                      B.DOBYear, B.PhoneCell, B.InstantIDScore, B.InstantIDHtml, B.DOB, B.GrossIncome, B.NonEmploymentIncome, B.NonEmploymentIncomeDesc, 
                      B.NonEmploymentIncomePhone, B.DriverLicense, B.DriverLicenseHtml, B.EMail, B.Language, L_B.Pos, A.Street, A.CityName, A.Zip, A.Zip0, 
                      A.StateCode, A.Phone, A.CityName + ', ' + A.StateCode + ' ' + A.Zip AS CityStateZip
FROM         dbo.t_Borrower AS B INNER JOIN
                      dbo.t_Loan_Borrower AS L_B ON B.ID = L_B.BorrowerID INNER JOIN
                      dbo.t_Loan AS L ON L_B.LoanID = L.ID INNER JOIN
                      dbo.t_Address AS A ON B.ID = A.BorrowerID AND A.Pos = 0
WHERE     (@LoanID IN (- 1, L_B.LoanID)) AND (@Pos IN (- 1, L_B.Pos))